package com.szholly.data.coderule.table;

import java.text.MessageFormat;
import java.util.HashMap;

import org.springframework.stereotype.Component;

import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.*;
import com.szholly.data.general.metadata.IBaseTable;

/**
 * 业务逻辑类
 */
@Component("DATA_RULEVALUE")
public class RuleValueTable extends InterceptorTableBase  implements IBaseTable{

	public final static String TABLE_NAME = "DATA_RULEVALUE";
	
	/**
	 * 获取单例对象
	 */
	public static RuleValueTable getSingleRef() {
		return (RuleValueTable) SpringBeanFactory.getBean("DATA_RULEVALUE");
	}
	
	@Override
	public String getMetadataTableName() {
		return TABLE_NAME;
	}
	
	@Override
	public String getTableGroup() {
		return "编码规则";
	}
	
	@Override
	public String getMetadataTableAliasName() {
		return "规则值存储表";
	}

	private HashMap<String, Field> fields;

	@Override
	protected HashMap<String, Field> fields() {
		if (fields == null) {
			fields = new HashMap<String, Field>();

			Field itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_GUID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(1);
			itemField.setFieldAliasName("全局ID");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_VERSION);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("数据版本");
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_COMPANY);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("企业名称");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_SYSTEMNAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("系统名称");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(RuleValueRow.FIELD_ID);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields
					.put(itemField.getFieldName().toUpperCase().trim(),
							itemField);

			itemField = new Field();
			itemField.setFieldName(RuleValueRow.FIELD_TABLEID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields
					.put(itemField.getFieldName().toUpperCase().trim(),
							itemField);

			itemField = new Field();
			itemField.setFieldName(RuleValueRow.FIELD_RULEINFOID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields
					.put(itemField.getFieldName().toUpperCase().trim(),
							itemField);

			itemField = new Field();
			itemField.setFieldName(RuleValueRow.FIELD_RULECONDITION);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(5);
			itemField.setFieldAliasName("");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields
					.put(itemField.getFieldName().toUpperCase().trim(),
							itemField);

			itemField = new Field();
			itemField.setFieldName(RuleValueRow.FIELD_RULEVALUE);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(6);
			itemField.setFieldAliasName("");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields
					.put(itemField.getFieldName().toUpperCase().trim(),
							itemField);

		}
		return fields;
	}

	/*
	 * 通过ID获取对象
	 */
	public static RuleValueRow GetRowByID(String id) {
		if (getSingleRef() == null)
			return null;
		IQueryFilter pQueryFilter = new QueryFilter();
		pQueryFilter.setWhereString(MessageFormat.format("{0} = {1}",
				RuleValueRow.FIELD_RULEINFOID, id));
		RuleValueRow rvRow = (RuleValueRow) getSingleRef()
				.getEntity(RuleValueRow.class, pQueryFilter);
		return rvRow;
	}

	/**
	 * 通过ID和查询条件获取对象
	 */
	public static RuleValueRow GetRowByIDAndStrWhere(String id, String strWhere) {
		if (getSingleRef() == null)
			return null;
		IQueryFilter pQueryFilter = new QueryFilter();
		String where = MessageFormat.format("{0} = {1} ",
				RuleValueRow.FIELD_RULEINFOID, id);

		if (!StringUtils.IsNullOrSpace(strWhere)) {
			where += MessageFormat.format(" and {0} = \'{1}\'",
					RuleValueRow.FIELD_RULECONDITION, strWhere);
		}

		pQueryFilter.setWhereString(where);
		RuleValueRow rowBase = getSingleRef().getEntity(RuleValueRow.class, pQueryFilter);
		return rowBase;
	}

	/*
	 * 清空表记录
	 */
	public static Boolean ClearTable() {
		if (getSingleRef() == null)
			return false;
		IQueryFilter pQueryFilter = new QueryFilter();
		getSingleRef().delete(pQueryFilter);
		return true;
	}
}
